﻿using HY.Common;
using HY.Entity;
using HY.Service;
using HY.Web.Controllers;
using HY.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace HY.Web.Areas.Monitor.Controllers
{
    public class EquipmentController : BaseController
    {
        BaseService service = new BaseService();
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Index(int page, int limit)
        {
            ReturnModel returnModel = new ReturnModel();
            try
            {
                var code = Request.Form["EquipmentCode"];
                var where = $"E.IsDeleted=0";
                if (!string.IsNullOrWhiteSpace(code))
                    where += $" AND E.EquipmentCode like '%{code}%'";
                var tuple = service.GetPageList<V_EquipmentList>("Equipment E LEFT JOIN EquipmentType ET ON E.EquipmentTypeID=ET.ID", "E.*,ET.TypeName", where, page, limit, "E.CreateDate desc");
                returnModel = base.GetReturnModel(tuple.Item1, tuple.Item2);
            }
            catch (Exception ex)
            {
                returnModel = base.GetReturnModel(ex);
                LogHelper.Error("查询设备列表", ex);
            }
            return Json(returnModel);
        }
        [HttpPost]
        [AllowAnonymous]
        public JsonResult SelectList()
        {
            ReturnModel returnModel = new ReturnModel();
            try
            {
                var list = service.EFGetMany<Equipment>(e => e.IsDeleted == false).Select(s => new { value = s.ID, text = s.EquipmentCode });
                returnModel = GetReturnModel(list.Count(), list);
            }
            catch (Exception ex)
            {
                returnModel = base.GetReturnModel(ex);
                LogHelper.Error("获取设备下拉列表失败", ex);
            }
            return Json(returnModel);
        }
    }
}